package com.zzzzzz.posts;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.zzzzzz.BaseDao;

@Repository
public class PostTagDao extends BaseDao{
	private final static String ADD = "insert into post_tag(post_id, tag_id, status) values(:postId, :tagId, :status)";
	private final static String EDIT_STATUS_BY_POSTSID = "update post_tag set status = ? where post_id = ?";
	private final static String EDIT_STATUS_BY_POSTSID_AND_TAGID = "update post_tag set status = ? where post_id = ? and tag_id = ?";
	private final static String FIND_TAGNAME_BY_POSTSID = "select t.name from post_tag pt inner join tag t on pt.tag_id = t.id where pt.post_id = ? and pt.status = 0 and t.status = 0 and t.up_id <> 0";
	
	public void add(PostTag postTag){
		update(ADD, postTag);
	}
	
	public void editStatusByPostId(Long postId, Integer status){
		update(EDIT_STATUS_BY_POSTSID, status, postId);
	}
	
	public void editStatusByPostIdAndTagId(Long postId, Integer tagId, Integer status){
		update(EDIT_STATUS_BY_POSTSID_AND_TAGID, status, postId, tagId);
	}
	
	public List<String> findTagNameByPostId(Long postId){
		return findList(
				FIND_TAGNAME_BY_POSTSID, 
				new RowMapper<String>(){
					public String mapRow(ResultSet rs, int rn)
							throws SQLException {
						return rs.getString("name");
					}
				}, 
				postId);
	}
	
}
